<%@page import="cn.egame.interfaces.gc.GameInfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="cn.egame.common.model.PageData"%>
<%@page import="java.util.*"%>
<%@page import="cn.egame.interfaces.pu.AppParameter"%>
<%@page import="cn.egame.interfaces.gc.GameInfo"%>  
<%@page import="cn.egame.interfaces.gc.GameTagLinkInfo"%> 
<%@page import="com.egame.manage.biz.server.pu.*"%> 
<%@page import="com.egame.manage.biz.utils.*"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/common.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/main.css"  />
<script type="text/javascript">

function getOptionValueList(selectId){
	var options = document.getElementById(selectId);
	var result = new Array();
	for(var i=0;i<options.length;i++){
		if(options[i].selected == true){
			result.push(options[i].value);
		}
	}
	return result;
}

function getOptionValue(selectName){
	var select = document.getElementById(selectName);
	if(select == null)return null;
	var options = select.options;
	if(options == null)return null;
	var optionIndex = select.selectedIndex;
	if(optionIndex == null)return null;
	return options[optionIndex].value;
}

function getOptionText(selectName){
	var select = document.getElementById(selectName);
	if(select == null)return null;
	var options = select.options;
	if(options == null)return null;
	var optionIndex = select.selectedIndex;
	if(optionIndex == null)return null;
	return options[optionIndex].text;
}

function setOptionText(selectName,text){
	var select = document.getElementById(selectName);
	if(select == null)return null;
	var options = select.options;
	if(options == null)return null;
	var optionIndex = select.selectedIndex;
	if(optionIndex == null)return null;
	options[optionIndex].text = text;
}

function removeOption(selectName){
	var select = document.getElementById(selectName);
	if(select == null)return null;
	var options = select.options;
	if(options == null)return null;
	var optionIndex = select.selectedIndex;
	if(optionIndex == null)return null;
	options.remove(optionIndex);
}

function addOption(selectName,value,text){
	var select = document.getElementById(selectName);
	if(select == null)return null;
	var options = select.options;
	options.add(new Option(text,value));		
	options[options.length-1].selected = "selected";
}

function moveOption(fromId,toId){
	var options = document.getElementById(fromId);
	var toOptions = document.getElementById(toId);
	for(var i=0;i<options.length;i++){
		if(options[i].selected == true){
			toOptions.add(new Option(options[i].text,options[i].value));
			options.remove(i);
			--i;
		}
	}
}

function moveToOption(fromId,toId){
	var options = document.getElementById(fromId);
	var toOptions = document.getElementById(toId);
	for(var i=0;i<options.length;i++){
		if(options[i].selected == true){
			toOptions.options.add(new Option(options[i].text,options[i].value),0);
			options.remove(i);
			--i;
		}
	}
}

	function addRecommendGame(url){
		var value = getOptionValueList("gameList");
		if(value==null || value==""){
			alert("请选择要添加的游戏");
			return;
		}
		document.getElementById("gid").value = value;
		document.getElementById("myform").action = '<%=request.getContextPath() %>' + url;
		document.getElementById("myform").submit();
	}
	function deleteRecommendGame(url){
		var value = getOptionValueList("recommendList");
		if(value==null || value==""){
			alert("请选择要移除的游戏");
			return;
		}
		document.getElementById("gid").value = value;
		document.getElementById("myform").action = '<%=request.getContextPath() %>' + url;
		document.getElementById("myform").submit();
	}
	function moveRecommendGame(url){
		var value = getOptionValueList("recommendList");		
		if(value==null || value==""){
			alert("请选择要移动的游戏");
			return;
		}else{
			var values = value.toString().split(",");
			if(values.length>1){
				alert("只能选择一个要移动的游戏");
				return;
			}			
		}
		document.getElementById("gid").value = value;
		document.getElementById("myform").action = '<%=request.getContextPath() %>' + url;		
		document.getElementById("myform").submit();
	}	
	
	function getFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].text.indexOf(flag)>=0?0:1);
			}
		}
		return result;
	}

	function setFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				if(options[i].text.indexOf(flag)>=0){
					options[i].text = options[i].text.split(flag)[1];
				}else{
					options[i].text = flag + options[i].text;
				}				
			}
		}
	}
	
	function setStart() {	
		$.ajax({			
			url : "<%=request.getContextPath() + "/store/home/recommend/startflag/set.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'hotflag' : getFlagList("recommendList","首发").toString()
			},
			success : function(msg) {	
				setFlagList("recommendList","首发--");				
			}
		});
	}

	function setNew() {	
		$.ajax({			
			url : "<%=request.getContextPath() + "/store/home/recommend/newflag/set.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'newflag' : getFlagList("recommendList","新品").toString()
			},
			success : function(msg) {	
				setFlagList("recommendList","新品--");	
			}
		});
	}
	
	function setRecommend() {
		$.ajax({			
			url : "<%=request.getContextPath() + "/store/home/recommend/recommendflag/set.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'newflag' : getFlagList("recommendList","推荐").toString()
			},
			success : function(msg) {	
				setFlagList("recommendList","推荐--");	
			}
		});
	}
</script>
<title>推荐列表</title>
</head>
<body>
	
<form id="myform" action="<%=request.getContextPath() + "/store/game/recommend/list.do" %>" method="post">
<input type="hidden" name=gid id=gid>
<% 	
	String gameName = "";
	if(request.getAttribute("gameName")!=null){
		gameName = (String)request.getAttribute("gameName");
	}
	String recommendName = "";
	if(request.getAttribute("recommendName")!=null){
		recommendName = (String)request.getAttribute("recommendName");
	}	
%>

<div class="mt10 pt10 clearfix">
<table>
	<tr>
		<td>
			<table>				
				<tr>
					<td width=35% align=center><p style="font-size: 16px;margin-bottom: 20px;font-weight: bold;">游戏列表</p></td>
					<td></td>
					<td width=30% align=center><p style="font-size: 16px;margin-bottom: 20px;font-weight: bold;">推荐列表</p></td>
					<td></td>					
				</tr>	
				<tr>
					<td colspan=2>
						游戏ID/游戏名称:<input type=text name="gname" value="<%=gameName %>">
						<input type="submit" value="查询" class="btn3">
					</td>
					<td colspan=2>
						游戏ID/游戏名称:<input type=text name="recommendname"  value="<%=recommendName %>">
						<input type=submit value="查询" class="btn3">
					</td>					
				</tr>			
				<tr>
					<td>
						<select name="gameList" id="gameList" size="20" style="width:240px;" multiple="multiple">
							<%
								if(request.getAttribute("gameList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("gameList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>												
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=bean.getOsCode() %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>
						<input type=button value="添加 >" class="btn3"  onclick="addRecommendGame('/store/game/recommend/add.do')"> <br/>
						<input type="button" class="btn3" onclick="deleteRecommendGame('/store/game/recommend/delete.do')" value="< 移除"><br/>
					</td>
					
					<td>
						<select name="recommendList" id="recommendList" size="20" style="width:240px;"  multiple="multiple">
							<%
							Map<Integer, GameTagLinkInfo> newMap = new HashMap<Integer, GameTagLinkInfo>();
							Map<Integer, GameTagLinkInfo> startMap = new HashMap<Integer, GameTagLinkInfo>();
							Map<Integer, GameTagLinkInfo> recoMap = new HashMap<Integer, GameTagLinkInfo>();
							if(request.getAttribute("newMap")!=null){
								newMap = (Map<Integer, GameTagLinkInfo>)request.getAttribute("newMap");
							}	
							if(request.getAttribute("startMap")!=null){
								startMap = (Map<Integer, GameTagLinkInfo>)request.getAttribute("startMap");
							}
							if(request.getAttribute("recoMap")!=null){
								recoMap = (Map<Integer, GameTagLinkInfo>)request.getAttribute("recoMap");
							}
								if(request.getAttribute("recommendList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("recommendList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>		
											<% if(startMap.containsKey(bean.getGId())){
												%>
												首发--
												<% } %>		
												<% if(newMap.containsKey(bean.getGId())){
												%>
												新品--
												<% } %>		
												<% if(recoMap.containsKey(bean.getGId())){
												%>
												推荐--
												<% } %>												
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=ParameterServiceImpl.getInstance().getParameterNameById(0,0L,ConstVar.APP_OS,""+bean.getOsCode()) %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>		
						<input type="button" class="btn3" onclick="setStart()" value="首发"><p>
						<input type="button" class="btn3" onclick="setNew()" value="新品"><p>
						<input type="button" class="btn3" onclick="setRecommend()" value="推荐"><p>					
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/upper.do')" value="上移"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/lower.do')" value="下移"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/top.do')" value="置顶"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/down.do')" value="置底"><p>						
						
					</td>					
				</tr>
			</table>
		</td>
	</tr>
</table>
</div>
</div>
</form>
</body>
</html>